﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CoreReset
{
    delegate void AddTextDelegate(string text);

    public partial class DebugLogForm : Form
    {
        public DebugLogForm(CoreResetForm mainForm)
        {
            m_mainForm = mainForm;
            InitializeComponent();
        }

        public DebugLogForm(CoreResetForm mainForm, string caption)
        {
            m_mainForm = mainForm;
            InitializeComponent();
            this.Text = caption;
        }
        public void AddText(string text)
        {
            if (this.textBoxAnalysisLog.InvokeRequired)
            {
                AddTextDelegate d = new AddTextDelegate(AddTextUnsafe);
                try
                {
                    this.Invoke(d, new object[] { text });
                }
                catch
                {

                }
            }
            else
            {
                AddTextUnsafe(text);
            }
        }

        private void AddTextUnsafe(string text)
        {
            string[] lines = text.Split('\n');

            foreach (string line in lines)
            {
                this.textBoxAnalysisLog.AppendText(line + "\n");
            }
        }

        CoreResetForm m_mainForm;
        private void DebugLogForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (m_mainForm != null)
            {
                m_mainForm.OnDebugLogFormClosing();
                if (e.CloseReason == CloseReason.UserClosing && this.Visible)
                {
                    this.Hide();
                    e.Cancel = true;
                }
            }
         }

        internal void ClearText()
        {
            textBoxAnalysisLog.Clear();
        }
    }
}
